<?php
class ControllerPost { 

    function getFunction($op){
        
        switch ($op) {
            case "new":
                $this->newPost();
                break;
            
            case "list":
                $this->listPost();
                break;
            
            case "save":
                $this->savePost();
                break;

            case "edit":
                $this->editPost();
                break;
            
            case "update":
                $this->updatePost();
                break;
            
            case "del":
                $this->deletePost();
                break;
            
            case "combo":
                return $this->comboPost();
                break;
            
            default:
                echo "ERROR NO HAY CONTROLADOR PARA ESA OPCION";
                break;
        }
    }
    

    function newPost(){
        include_once "vista/vista.post.php";
        form_post();
    }    
    

    function savePost(){
        include_once "modelo/modelo.post.php";
        
        $p = new post();
        $p->set_p_id($_REQUEST["p_id"]); 
        $p->set_p_u_id($_REQUEST["p_u_id"]); 
        $p->set_p_date($_REQUEST["p_date"]); 
        $p->set_p_desc($_REQUEST["p_desc"]); 
        $p->set_p_c_count($_REQUEST["p_c_count"]); 
        $p->set_p_l_count($_REQUEST["p_l_count"]); 
        $p->set_p_s_count($_REQUEST["p_s_count"]); 
        $p->set_p_ph_id($_REQUEST["p_ph_id"]); 
        $p->set_p_w_id($_REQUEST["p_w_id"]); 
        $p->set_p_status($_REQUEST["p_status"]); 

        
        if($p->Save()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_save_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_save_error']." </div>";
        }
        $this->listPost($msj);
        
    }    
    
    function editPost(){
        include_once 'modelo/modelo.post.php';
        include_once 'vista/vista.post.php';
        
        $p = new post();
        $p->setp_id($_REQUEST["p_id"]); 
        
        $p->getData();
        
        form_post($p);
        
    }    
    
    function deletePost(){
        include_once 'modelo/modelo.post.php';
        
        $p = new post();
        $p->setp_id($_REQUEST["p_id"]); 
        
        if($p->Delete()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_delete_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_delete_error']." </div>";
        }
        $this->listPost($msj);
    }

    
    function updatePost(){
        include_once 'modelo/modelo.post.php';
        
        $p = new post();
        $p->set_p_id($_REQUEST["p_id"]); 
        $p->set_p_u_id($_REQUEST["p_u_id"]); 
        $p->set_p_date($_REQUEST["p_date"]); 
        $p->set_p_desc($_REQUEST["p_desc"]); 
        $p->set_p_c_count($_REQUEST["p_c_count"]); 
        $p->set_p_l_count($_REQUEST["p_l_count"]); 
        $p->set_p_s_count($_REQUEST["p_s_count"]); 
        $p->set_p_ph_id($_REQUEST["p_ph_id"]); 
        $p->set_p_w_id($_REQUEST["p_w_id"]); 
        $p->set_p_status($_REQUEST["p_status"]); 

        if($p->Update()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_update_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_update_error']." </div>";
        }
        $this->listPost($msj);
    }

    
    function listPost($msj=false){
        include_once 'modelo/modelo.post.php';
        include_once 'vista/vista.post.php';
        
        $p = new post();

        $p->setp_id($_REQUEST["p_id"]); 
        $p->getData();
        
        $dataTotal = $p->GetList(" count(*) ", false,  true);
        $dataTotal = $dataTotal[0][0]; 
        $data = $p->GetList(" * ", false);
        if(is_null($data)){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_empty_error']." </div>";
        }
        list_post($data, $msj, $dataTotal);

    }

}